﻿#ifndef STRINGTOOL_H
#define STRINGTOOL_H

#include <QString>
#include <QList>
#include <QDebug>
#include "config.h"

#define QT_USE_FAST_CONCATENATION
#define QT_USE_FAST_OPERATOR_PLUS


class StringTool
{
    public:

        static inline int min (const int a, const int b) {
            if (a < b) return a; return b;
        }

        static inline int max (const int a, const int b) {
            if (a > b) return a; return b;
        }

        static inline int min (const int a, const int b, const int c) {
            return min (a, min (b, c));
        }

        static inline int abs (const int a) {
            if (a < 0) return -a; return a;
        }

        static inline int min (const int a, const int b, const int c, const int d) {
            return min (min (a, b), min (c, d));
        }

        static QString vowels () { return "AEIOUY"; }
        static QString soundexLettersToDelete () { return "AEHIOUWY"; }

        static bool isAVowel (const QChar& car) { return vowels ().contains (car); }
        static bool isAConsonant (const QChar& car) { return ! isAVowel (car); }

        static QString soundexCode (const QString &word);
        static char soundexInternalCode (const char& car);
        static QString removeFrom (const QString& word, const QString& letters);

        static QString uniqueLetters (const QString& word);
        static QString ordered (const QString& word);	// renvoie les lettres contenues dans le mot en ordre alphab
        static QString orderedUnique (const QString& word);

        static int dlDistance (const QString &str1, const QString &str2, bool damerau, const int& maxDiff/* = 99999*/);
};

//------------------------------------------------------------
inline QString StringTool::uniqueLetters (const QString &word)
{
    QString ret;

    QString::const_iterator iter;

    for (iter = word.constBegin () ; iter != word.constEnd () ; iter++)
    {
        const QChar& car = *iter;
        if (! ret.contains (car))
        {
            ret += car;
        }
    }

    return ret;
}
//------------------------------------------------------
inline QString StringTool::ordered (const QString &word)
{

    QString result (word);
    qSort (result.begin (), result.end ());

    return result;
}
//------------------------------------------------------------
inline QString StringTool::orderedUnique (const QString &word)
{
    QString result;

    for (int car = 'A' ; car <= 'Z' ; car++)
    {
        if (word.contains (car))
        {
            result += car;
        }
    }

    return result;
}



#endif // STRINGTOOL_H

